/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hero.web.entity;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author Volcano
 */
public class Pagination {

    private static Log log = LogFactory.getLog(Pagination.class);
    // 一页默认显示10条
    public static final int DEFAULT_PAGE_SIZE = 10;
    // 当前页
    private int pageIndex;
    // 每页显示多少条
    private int pageSize;
    // 总记录
    private int totalCount;
    // 总页数
    private int pageCount;


    public int getPageCount() {
        return pageCount;
    }

    /**
     * 此构造初始化当前页和每页的条数
     *
     * @param pageIndex
     *            当前页
     * @param pageSize
     *            一页多少条
     */
    public Pagination(int pageIndex, int pageSize) {
        if (pageIndex < 1) {
            pageIndex = 1;
            if (log.isDebugEnabled()) {
                log.debug("由于当前页小于1,所以让当前页等于" + pageIndex);
            }
        }
        if (pageSize < 1) {
            pageSize = 1;
            if (log.isDebugEnabled()) {
                log.debug("由于每页的记录数小于1,所以让每页的记录数等于" + pageSize);
            }
        }
        this.pageIndex = pageIndex;
        if(log.isDebugEnabled())
        {
            log.debug("当前页:"+pageIndex);
        }
        this.pageSize = pageSize;
        if(log.isDebugEnabled())
        {
            log.debug("每页行数:"+this.pageSize);
        }
    }

    /**
     * 此构造使用默认的pageSize去初始化每页的记录数
     *
     * @param pageIndex
     */
    public Pagination(int pageIndex) {
        this(pageIndex, DEFAULT_PAGE_SIZE);
    }

    /**
     * 返回第一行记录的起始位置
     */
    public int getFirstResult() {
        return (pageIndex - 1) * pageSize;
    }

    /**
     * 判断有没有上一页
     */
    public boolean hasPrevious() {
        return pageIndex > 1;
    }

    /**
     * 判断有没有下一页
     */
    public boolean hasNext() {
        return pageIndex < pageCount;
    }

    private void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public int getPageSize() {
        return pageSize;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
        // 通过总记录计算页数
        setPageCount(totalCount / pageSize
                + (totalCount % pageSize == 0 ? 0 : 1));
        if (log.isDebugEnabled()) {
            log.debug("共" + this.getPageCount() + "页");
        }
        if (totalCount == 0) {
            this.pageIndex = 1;
            if (log.isDebugEnabled()) {
                log.debug("总记录数为0,强制把当前页设置为1");
            }
        } else {
            if (pageIndex > pageCount) {
                this.pageIndex = this.pageCount;
                if (log.isDebugEnabled()) {
                    log.debug("当前页大于总页数,强制把当前页设置成最后一页!");
                }
            }
        }
    }

    /**
     * 判断有没有数据
     */
    public boolean isEmpty() {
        return getTotalCount() == 0;
    }
}
